package cn.iocoder.yudao.module.yyzx.service.nurser;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.yyzx.controller.admin.nurser.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.nurser.NurserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.yyzx.dal.mysql.nurser.NurserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.yyzx.enums.ErrorCodeConstants.*;

/**
 * 护工 Service 实现类
 *
 * @author lovedream
 */
@Service
@Validated
public class NurserServiceImpl implements NurserService {

    @Resource
    private NurserMapper nurserMapper;

    @Override
    public Integer createNurser(NurserSaveReqVO createReqVO) {
        // 插入
        NurserDO nurser = BeanUtils.toBean(createReqVO, NurserDO.class);
        nurserMapper.insert(nurser);
        // 返回
        return nurser.getId();
    }

    @Override
    public void updateNurser(NurserSaveReqVO updateReqVO) {
        // 校验存在
        validateNurserExists(updateReqVO.getId());
        // 更新
        NurserDO updateObj = BeanUtils.toBean(updateReqVO, NurserDO.class);
        nurserMapper.updateById(updateObj);
    }

    @Override
    public void deleteNurser(Integer id) {
        // 校验存在
        validateNurserExists(id);
        // 删除
        nurserMapper.deleteById(id);
    }

    private void validateNurserExists(Integer id) {
        if (nurserMapper.selectById(id) == null) {
            throw exception(NURSER_NOT_EXISTS);
        }
    }

    @Override
    public NurserDO getNurser(Integer id) {
        return nurserMapper.selectById(id);
    }

    @Override
    public PageResult<NurserDO> getNurserPage(NurserPageReqVO pageReqVO) {
        return nurserMapper.selectPage(pageReqVO);
    }

}